<?php
namespace app\admin\controller;

use app\admin\model\Platform as PlatformModel;
use Request;

/**
 * 平台信息
 */
class Platform extends Base
{

    public $data;
    /**
     * 显示文章树形分类列表
     * @return array 文章分类二维数组
     */
    // public function index()
    // {

    //     $list = PlatformModel::select();
    //     $this->assign('list', $list);
    //     return $this->fetch();
    // }
    // public $data;
    public function index($act = "")
    {
        $where = [];
        if (Request::has('search')) {
            $where[] = ['name', 'like', '%' . trim(Request::param('search/s', '', 'strip_tags')) . '%'];
        }
        //$list = PartitionModel::alias('a')->field('a.*,b.title as typename')->join('arctype b', 'b.id = a.typeid', 'left')->order('id desc')->where($where)->paginate(20);
        $list   = PlatformModel::order('id desc')->where($where)->paginate(config('webconfig.page'), false, ['query' => Request::param()]);
        $search = Request::param('search/s', '', 'strip_tags') ? Request::param('search/s', '', 'strip_tags') : "";
        $this->assign([
            'list'   => $list,
            'page'   => $list->render(),
            'search' => $search,
        ]);
        return $this->fetch();
    }

    /**
     * 添加分类信息
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Platform');
            if (true !== $result) {
                return $this->error($result);
            }
            $post['gamedb'] = PlatformModel::dbstr($post, 'game') ? PlatformModel::dbstr($post, 'game') : "";
            $post['userdb'] = PlatformModel::dbstr($post, 'user') ? PlatformModel::dbstr($post, 'user') : "";
            $post['logdb']  = PlatformModel::dbstr($post, 'log') ? PlatformModel::dbstr($post, 'log') : "";


            //$data = PlatformModel::create($post);
            $data           = new PlatformModel;

            //print_r($post['gamedb']);
           $model = $data->allowField(true)->save($post);

            if (!empty($model)) {
                return $this->success('添加成功', 'admin/Platform/index');
            }
        }
        $data = PlatformModel::select();
        //$tree = new Tree();
        //$data = $tree->tree($data, 'title');
        $this->assign('typename', $data);
        return $this->fetch();
    }

    /**
     * 修改分类信息
     * @return [type] [description]
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
//echo "123";
            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Platform');
            if (true !== $result) {
                return $this->error($result);
            }

            //$data = PlatformModel::edit($id, $post);
            $post['gamedb'] = PlatformModel::dbstr($post, 'game') ? PlatformModel::dbstr($post, 'game') : "";
            $post['userdb'] = PlatformModel::dbstr($post, 'user') ? PlatformModel::dbstr($post, 'user') : "";
            $post['logdb']  = PlatformModel::dbstr($post, 'log') ? PlatformModel::dbstr($post, 'log') : "";

            $data           = new PlatformModel;
 
            // 过滤post数组中的非数据表字段数据
           $model =  $data->allowField(true)->save($post, ['id' => $post['id']]);

            if ($model) {
                //addlog($id, $post['title']);
                return $this->success('修改成功', 'admin/Platform/index');
            } else {
                return $this->error('修改失败');
            }
        }

        $info = PlatformModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}
        $this->assign([
            //'typename' => $data,
            'info'   => $info,
            'gamedb' => !empty($info['gamedb']) ? unserialize($info['gamedb']) : "",
            'userdb' => !empty($info['userdb']) ? unserialize($info['userdb']) : "",
            'logdb'  => !empty($info['logdb']) ? unserialize($info['logdb']) : "",
            //'typepid'  => $info['pid'],
        ]);
        return $this->fetch();
    }

    /**
     * 删去分类信息
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = PlatformModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/Platform/index', '', 1);
            }
        }
        return $this->error('删去失败');
    }
    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        //$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
            if (input('?post.status')) {
                $data = ['id' => Request::post('id/d'), 'status' => Request::post('status')]; //状态
            }
            $model = PlatformModel::update($data);
            //echo $model;
            if (!empty($model)) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        }
    }
}
